# Contributor: fauxpark <fauxpark@gmail.com>

_realname=dht
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.27
pkgrel=1
pkgdesc="BitTorrent DHT library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url='https://github.com/jech/dht'
license=('MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=(https://github.com/jech/dht/archive/refs/tags/${_realname}-${pkgver}.tar.gz
        dht-cmake.patch::https://github.com/transmission/dht/commit/b02da598.patch)
sha256sums=('caba469a784a5c359c084099fdc025cfe09b1faec2ba9ba257b7384351c43c0a'
            '91fb75029bf04456bb7fd9c7cc14d544e906d35a309cc8de5be081049aeb7649')

prepare() {
  cd "${srcdir}"/dht-dht-${pkgver}

  patch -p1 -i ${srcdir}/dht-cmake.patch # Add cmake support
}

build() {
  cd "${srcdir}"/dht-dht-${pkgver}
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  cmake -B . -S ${srcdir}/dht-dht-${pkgver} \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_INSTALL_INCLUDEDIR=include/dht

  cmake --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" cmake --install .

  install -Dm644 "${srcdir}/dht-dht-${pkgver}/LICENCE" "${pkgdir}${MINGW_PREFIX}/share/licenses/dht/LICENSE"
}
